<template>
	<div>
		<!--
    	作者：侯念晨
    	时间：2017-08-07
    	描述：前端导出excel
    	目前只用于原生的table，还没有试过element的。excel表格样式在template中添加
    -->
    <a id="dlink"  style="display:none;"></a>
	</div>
</template>

<script>
	export default {
		data: function() {
			return {
				idTmr: "",
				end: true,
			}
		},
		props: {
			//表格Id
			tableId: '',
			//表格路径在页面中
			template: '',
			//excel名字
			excelName:'',
		},
		watch: {
			tableId: function() {
				if(this.tableId) {
					this.processOut(this.tableId);
				}
			},
			end: function() {
				this.$emit('backFunction');
			},
		},
		methods: {
			//获取浏览器
			getExplorer: function() {
				var explorer = window.navigator.userAgent;
				//ie 
				if(explorer.indexOf("MSIE") >= 0) {
					return 'ie';
				}
				//firefox 
				else if(explorer.indexOf("Firefox") >= 0) {
					return 'Firefox';
				}
				//Chrome
				else if(explorer.indexOf("Chrome") >= 0) {
					return 'Chrome';
				}
				//Opera
				else if(explorer.indexOf("Opera") >= 0) {
					return 'Opera';
				}
				//Safari
				else if(explorer.indexOf("Safari") >= 0) {
					return 'Safari';
				}
			},
			processOut: function(tableid) { //整个表格拷贝到EXCEL中
				debugger;
				if(this.getExplorer() == 'ie') {
					var curTbl = document.getElementById(tableid);
					var oXL = new ActiveXObject("Excel.Application");

					//创建AX对象excel 
					var oWB = oXL.Workbooks.Add();
					//获取workbook对象 
					var xlsheet = oWB.Worksheets(1);
					//激活当前sheet 
					var sel = document.body.createTextRange();
					sel.moveToElementText(curTbl);
					//把表格中的内容移到TextRange中 
					sel.select;
					//全选TextRange中内容 
					sel.execCommand("Copy");
					//复制TextRange中内容  
					xlsheet.Paste();
					//粘贴到活动的EXCEL中       
					oXL.Visible = true;
					//设置excel可见属性

					try {
						var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
					} catch(e) {
						print("Nested catch caught " + e);
					} finally {
						oWB.SaveAs(fname);

						oWB.Close(savechanges = false);
						//xls.visible = false;
						oXL.Quit();
						oXL = null;
						//结束excel进程，退出完成
						//window.setInterval("Cleanup();",1);
						this.$data.idTmr = window.setInterval("Cleanup();", 1);

					}

				} else {
					this.tableToExcel(this.tableId)
				}
			},
			Cleanup: function() {
				window.clearInterval(this.$data.idTmr);
				CollectGarbage();
			},
			tableToExcel: function() {
				debugger;
				var uri = 'data:application/vnd.ms-excel;base64,';
				var template = this.template;
				var name = this.excelName;
				//var template = '<template><div><div><table>{table}</table></div></div></template>';
				return this.exports(name, template, uri)
			},
			base64: function(s) {
				debugger;
				return window.btoa(unescape(encodeURIComponent(s)));
			},
			format: function(s, c) {
				debugger;
				return s.replace(/{(\w+)}/g,
					function(m, p) {
						return c[p];
					})
			},
			exports: function(name, template, uri) {
				debugger;
				var table = document.getElementById(this.tableId);
				var ctx = {
					worksheet: name || 'Worksheet',
					table: table.innerHTML
				}
				var returnData = this.format(template, ctx)
				var mmp = this.base64(returnData);
				document.getElementById("dlink").href = uri + mmp;
				document.getElementById("dlink").download = name;
				document.getElementById("dlink").click();
				//				window.location.href = uri + mmp;
				this.$data.end = false;
			},
		}
	}
</script>

<style>

</style>